package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RAuditBreakageBean;
import com.xoms.admin.controller.res.bean.RAuditCheckBean;
import com.xoms.admin.controller.res.bean.RAuditPurchaseBean;
import com.xoms.admin.controller.res.bean.RAuditPureBackBean;
import com.xoms.admin.controller.res.bean.RAuditStockInBean;
import com.xoms.admin.controller.res.bean.RProAuditBean;
import com.xoms.admin.controller.springmvc.req.GetAuditBreakageParam;
import com.xoms.admin.controller.springmvc.req.GetAuditCheckParam;
import com.xoms.admin.controller.springmvc.req.GetAuditPurchaseParam;
import com.xoms.admin.controller.springmvc.req.GetAuditPureBackParam;
import com.xoms.admin.controller.springmvc.req.GetAuditStockInParam;
import com.xoms.admin.controller.springmvc.req.GetProAudits4AccountParam;
import com.xoms.admin.controller.springmvc.req.GetProAuditsParam;
import com.xoms.admin.controller.springmvc.req.ProAuditParam;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.ProAuditService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

/**
 * 商品审核相关接口
 * @author chengl
 *
 */
@Component
public class ProAuditController {
	
	@Autowired
	ProAuditService proAuditServiceImpl;
	
	/**
	 * 获取审核列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProAuditBean> getAudits(GetProAuditsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProAuditBean> result = new ListResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			int type = NumUtil.toInt(param.getType(), 0);// 审核类型：1采购2入库3报损4退货到供应商5盘库
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();// 审核时间段，起始时间
			String endTime = param.getEndTime();// 审核时间段，截止时间
			
			// 获取审核列表
			PageResult<RProAuditBean> resData = proAuditServiceImpl.getProAudits(account, type, page,
					PageSize, startTime, endTime);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取当前账号审核列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProAuditBean> getAudits4Account(GetProAudits4AccountParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProAuditBean> result = new ListResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			int type = NumUtil.toInt(param.getType(), 0);// 审核类型：1采购2入库3报损4退货到供应商5盘库
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();// 审核时间段，起始时间
			String endTime = param.getEndTime();// 审核时间段，截止时间
			
			// 获取审核列表
			PageResult<RProAuditBean> resData = proAuditServiceImpl.getProAudits4Acc(account, type, page,
					PageSize, startTime, endTime);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 根据类型审核不同，进行不同操作审核
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult audit(ProAuditParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String aid = param.getAid();// 审核id
			int status = NumUtil.toInt(param.getStatus(), 0);// 审核状态:1通过2未通过0待审核
			String remark = param.getRemark();
			boolean flag = proAuditServiceImpl.audit(account, aid, status, remark);// 审核
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}	
	
	/**
	 * 获取采购信息和审核详情列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAuditPurchaseBean> getPurchase(GetAuditPurchaseParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAuditPurchaseBean> result = new ObjectResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String aid = param.getAid();
			
			// 获取采购信息和审核详情列表
			RAuditPurchaseBean resData = proAuditServiceImpl.getPurchase(account, aid);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		
		return result;
	}
	
	/**
	 * 获取审核信息和入库详情列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAuditStockInBean> getStockIn(GetAuditStockInParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAuditStockInBean> result = new ObjectResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String aid = param.getAid();
			
			// 获取审核信息和入库详情列表
			RAuditStockInBean resData = proAuditServiceImpl.getStockIn(account, aid);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		
		return result;
	}
	
	/**
	 * 获取审核信息和报损详情列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAuditBreakageBean> getBreakage(GetAuditBreakageParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAuditBreakageBean> result = new ObjectResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String aid = param.getAid();
			
			// 获取审核信息和报损详情列表
			RAuditBreakageBean resData = proAuditServiceImpl.getBreakage(account, aid);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		
		return result;
	}
	
	/**
	 * 获取盘库审核列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAuditCheckBean> getCheck(GetAuditCheckParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAuditCheckBean> result = new ObjectResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String aid = param.getAid();
			
			// 获取审核信息和盘库详情列表
			RAuditCheckBean resData = proAuditServiceImpl.getCheck(account, aid);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		
		return result;
	}
	
	/**
	 * 获取退货审核
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAuditPureBackBean> getPureBack(GetAuditPureBackParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAuditPureBackBean> result = new ObjectResult<>();

		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String aid = param.getAid();
			// 获取审核信息和退货审核详情列表
			RAuditPureBackBean resData = proAuditServiceImpl.getPureBack(account, aid);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		
		return result;
	}
}
